/**
 * 
 */
package nl.boektrust.btsoep.client;

import java.util.Date;

import com.google.appengine.api.utils.SystemProperty.Environment.Value;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author rao3
 *
 */
public abstract class EditBookPanel extends VerticalPanel  {
	private Label label = new Label("");
	private boolean isInEditMode;
	HorizontalPanel displayPanel=  new HorizontalPanel();
	HorizontalPanel editPanel = new HorizontalPanel();
	Label theLabel= new Label("");
	private boolean isEditable = true;


	


	/**
	 * @return the isEditable
	 */
	public boolean isEditable() {
		return isEditable;
	}

	/**
	 * @param isEditable the isEditable to set
	 */
	public void setEditable(boolean isEditable) {
		this.isEditable = isEditable;
	}

	public EditBookPanel(String label, boolean isInEditMode) {
		this.label = new Label(label);
		this.isInEditMode = isInEditMode;
		this.add(displayPanel);

		theLabel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if ((Label)event.getSource() == theLabel) {
					if (isEditable == true) setToEditMode();
				}
			}
		});

		displayPanel.add(theLabel);
		this.add(editPanel);
		if (isInEditMode == false) {
			displayPanel.setVisible(true);
			editPanel.setVisible(false);
		} else {		
			displayPanel.setVisible(false);
			editPanel.setVisible(true);
		}
		
	}

	public EditBookPanel(boolean isInEditMode) {
		this("", isInEditMode);
	}

	public EditBookPanel(String label) {
		this(label, false);
	}

	public EditBookPanel() {
		this(false);
	}
	
	/**
	 * @return the isInEditMode
	 */
	public boolean isInEditMode() {
		return isInEditMode;
	}

	/**
	 * @param isInEditMode the isInEditMode to set
	 */
	public void setInEditMode(boolean isInEditMode) {
		this.isInEditMode = isInEditMode;
	}

	public void toggleEditMode() {
		if (this.isInEditMode == true) {
			this.isInEditMode = false;
			setToDisplayMode();
		} else {
			this.isInEditMode = true;
			setToEditMode();
		}
	}
	
	public void clearPanel() {
		for (int i=this.getWidgetCount()-1; i>=0; i--) {
			this.remove(i);
		}
	}
	
	void setToEditMode() {
		editPanel.setVisible(true);
		displayPanel.setVisible(false);
	}

	void setToDisplayMode() {
		displayPanel.setVisible(true);
		editPanel.setVisible(false);;
		if (theLabel == null || theLabel.getText().length() == 0) {
			theLabel.setText("nog niet ingevuld");
		}
	}
	
	String labelFormatter(String str) {
		return str;
	}
	
	String labelFormatter(int i) {
		return new Integer(i).toString();
	}
	
	boolean hasInvalidData() {
		return false;
	}

	/**
	 * @return the label
	 */
	public Label getLabel() {
		return label;
	}

	/**
	 * @param label the label to set
	 */
	public void setLabel(Label label) {
		this.label = label;
	}
	

}
